package com.wanxi.dangdang.controller.front.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.model.pojo.CUser;
import com.wanxi.dangdang.model.vo.CUserVo;
import com.wanxi.dangdang.model.vo.LoginVo;
import com.wanxi.dangdang.service.WeChatService;
import com.wanxi.dangdang.service.redis.RedisService;
import com.wanxi.dangdang.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/8/30 0030
 * @Version: 1.0
 */
@Api(tags = "C端用户后端管理")
@RestController
@RequestMapping("/front/c/weChat")
@Slf4j
public class WeChatController {
    @Autowired
    private WeChatService weChatService;

    @Autowired
    private RedisService redisService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;


    @PostMapping("/loginOrRegister")
    @ApiOperation("C端用户账号密码登陆")
    private CommonResult loginOrRegister(@RequestBody LoginVo loginVo){
        if(StringUtils.isBlank(loginVo.getAccount())){
            log.info("C端用户账号密码登陆 账号为空");
            return CommonResult.failed("账号不能为空");
        }
        log.info("C端用户账号密码登陆 数据"+ JSONObject.toJSONString(loginVo));
        HashMap<String, String> map = weChatService.loginPassOrRegister(loginVo);
        if (ObjectUtils.isEmpty(map)){
            log.info("C端用户账号密码登陆 错误");
            return CommonResult.failed("密码错误");
        }
        return CommonResult.successData(map);
    }

    @PostMapping("/loginCode")
    @ApiOperation("C端用户手机登陆")
    private CommonResult loginCode(@RequestBody LoginVo loginVo){
        if(StringUtils.isBlank(loginVo.getAccount())){
            log.info("C端用户账号密码登陆 账号为空");
            return CommonResult.failed("账号不能为空");
        }
        log.info("C端用户手机登陆 数据"+ JSONObject.toJSONString(loginVo));
        String redisCode = redisService.get("codeTel-" + loginVo.getAccount());
        if (!StringUtils.equals(redisCode,loginVo.getTelCode())||StringUtils.isBlank(loginVo.getTelCode())){
            return CommonResult.failed("手机验证码错误");
        }
        HashMap<String, String> map = weChatService.loginCode(loginVo);
        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failed("该账号不存在/或已注销");
        }
        return CommonResult.successData(map);
    }

    @GetMapping("/phoneCode")
    @ApiOperation("C端用户手机号获取验证码")
    private void phoneCode(@ApiParam(value = "电话号码", required = true)@RequestParam("account") String phone) throws Exception {
        log.info("C端用户手机号获取验证码 数据"+ JSONObject.toJSONString(phone));
        weChatService.getPhoneCode(phone);
    }

    @GetMapping("/findById")
    @ApiOperation("C端用户id查找C端用户（需要登录）")
    private CommonResult<CUser> findById(HttpSession httpSession){
        Long id = (Long) httpSession.getAttribute("id");
        if (id == null){
            CommonResult.failed("请登录");
        }
        CUser cUser = weChatService.findById(id);
       return CommonResult.successData(cUser);
    }
}
